package inventory.controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import utilites.Constants;
import utilites.ViewUtilites;
import dto.MaterialMaster;
import inventory.daoImplementer.CategoryMasterImplementor;
import inventory.daoImplementer.MaterialMasterImplementor;
import inventory.daoInterface.CategoryMasterDao;
import inventory.daoInterface.MaterialMasterDao;
import inventory.model.CategoryMaster;
import inventory.view.CategoryManagment;


public class CategoryManagmentControler implements ActionListener,ItemListener
{
	private CategoryMaster model;
	private CategoryManagment view;
	CategoryMasterDao cmd;
	
	public CategoryManagmentControler() 
	{
		
	}
	
	public CategoryManagmentControler(CategoryMaster model, CategoryManagment view) 
	{
		super();
		this.model = model;
		this.view = view;
		
		view.setActionlistener(this);
		view.getEditCbName().addItemListener(this);
		
		cmd=new CategoryMasterImplementor();
	}
	
	public void reactOnAdd()
	{
		String name=view.getAddTfName().getText();
		String code=name;
		
		model=new CategoryMaster(code, name, Constants.ACTIVE);
		CategoryMasterDao imp=new CategoryMasterImplementor();
		
		try
		{
			imp.add(model);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
		
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Catagory Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
	}
	
	
	public void reactOnUpdate()
	{
		String name=view.getEditCbName().getSelectedItem().toString();
		String newName=view.getEditTfNewname().getText();
		
		CategoryMasterDao cd=new CategoryMasterImplementor();
		model= cd.retreive("catName", name);
		model.setCatName(newName);
		
		
		if(view.getRdbtnActive().isSelected())
			model.setStatus(Constants.ACTIVE);
		
		if(view.getRdbtnInactive().isSelected())
			model.setStatus(Constants.INACTIVE);
		
		CategoryMasterDao imp1=new CategoryMasterImplementor();
		
		try
		{
			imp1.update(model);
			JOptionPane.showMessageDialog(null, "Data successfully Updated.");
		}
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Catagory Name already exist, please use another one");
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully Added,Please try again !");
		}
		
	}

	public void fillComboBoxCatagoryName()
	{
		String[] catNames=null;
		CategoryMaster[] cm=null;
		
		cm=cmd.retreiveAll();
		
		catNames=new String[cm.length+1];
		
		catNames[0]="--Select Name--";
		
		for(int i=1;i<=cm.length;i++)
		{
			catNames[i]=cm[i-1].getCatName();
		}
		
		ViewUtilites.set_comboBox(view.getEditCbName(),catNames);
		ViewUtilites.setAutoSuggestionsComboBox(view.getEditCbName());
	}
	
	
	public void setRadioBtn(String itemName)
	{
		CategoryMaster cm=cmd.retreive("catName",itemName);
		
		if(cm.getStatus()==Constants.ACTIVE)
		{
			view.getRdbtnActive().setSelected(true);
		}
		
		if(cm.getStatus()==Constants.INACTIVE)
		{
			view.getRdbtnInactive().setSelected(true);
		}
	}
	
	//*********************************************************************************
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		
		if(k.contains("add"))
		{
			reactOnAdd();
		}
		
		if(k.contains("update"))
		{
			reactOnUpdate();
		}
	}

	
	@Override
	public void itemStateChanged(ItemEvent event) 
	{
		if (event.getStateChange() == ItemEvent.SELECTED) 
		{
	          Object item = event.getItem();
	          String itemName=item.toString();
	          setRadioBtn(itemName);
	    }
	}
	
}
